package com.honglu.backend.controller;

import com.honglu.backend.bo.NewGuestStatisticalChannelBo;
import com.honglu.backend.service.impl.NewGuestStatisticalChannelServiceImpl;
import com.honglu.backend.utils.CodeUtils;
import com.honglu.backend.utils.JSONUtils;
import com.honglu.backend.vo.StatisticalChannelVo;
import com.honglu.utils.DateUtils;
import com.honglu.utils.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by tangbin on 2017/6/12.
 */
@Controller
@RequestMapping(value ="/backend/web/statisticalChannel")
public class NewGuestStatisticalChannelController {

    private static Logger logger = Logger.getLogger(NewGuestStatisticalChannelController.class);

    @Autowired
    private NewGuestStatisticalChannelServiceImpl newGuestStatisticalChannelService;

    /**
     * 渠道新客统计
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getNewGuestStatisticalChannel.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void getNewGuestStatisticalChannel(HttpServletRequest request, HttpServletResponse response, StatisticalChannelVo vo){
        try {
            if (StringUtils.isNotBlank(vo.getCreateEndTime())) {
                SimpleDateFormat df  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                vo.setCreateEndTime(df.format(DateUtils.getFirstTimeOfDay(df.parse(vo.getCreateEndTime()))));
            }
            //获取count
            Integer count = newGuestStatisticalChannelService.getNewGuestStatisticalChannelCount(vo);
            //获取统计list
            List<NewGuestStatisticalChannelBo> list = newGuestStatisticalChannelService.getNewGuestStatisticalChannelList(vo);
            JSONUtils.toListJSON(response, list, count, CodeUtils.QUERRYSUCCESS);
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            JSONUtils.toJSON(response, CodeUtils.QUERRYFAIL);
            return ;
        }
    }

    /**
     * 渠道新客汇总
     * @param request
     * @param response
     */
    @RequestMapping(value = "/getNewGuestSummaryChannel.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void getNewGuestSummaryChannelList(HttpServletRequest request, HttpServletResponse response, StatisticalChannelVo vo){
        try {
            if (StringUtils.isNotBlank(vo.getSourceString())) {
                if ("iPhone".equals(vo.getSourceString())) {
                    vo.setSource(1);
                } else if ("iPad".equals(vo.getSourceString())) {
                    vo.setSource(2);
                } else if ("Android".equals(vo.getSourceString())) {
                    vo.setSource(3);
                } else if ("微信".equals(vo.getSourceString())) {
                    vo.setSource(4);
                } else if ("H5".equals(vo.getSourceString())) {
                    vo.setSource(5);
                }
            }
            if (StringUtils.isNotBlank(vo.getCreateEndTime())) {
                SimpleDateFormat df  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                vo.setCreateEndTime(df.format(DateUtils.getFirstTimeOfDay(df.parse(vo.getCreateEndTime()))));
            }
            //获取count
            Integer count = newGuestStatisticalChannelService.getNewGuestSummaryChannelCount(vo);
            //获取统计list
            List<NewGuestStatisticalChannelBo> list = newGuestStatisticalChannelService.getNewGuestSummaryChannelList(vo);
            JSONUtils.toListJSON(response, list, count, CodeUtils.QUERRYSUCCESS);
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            JSONUtils.toJSON(response, CodeUtils.QUERRYFAIL);
            return ;
        }
    }


    /**
     * excel导出渠道新客统计
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportNewGuestStatisticalChannel.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void exportNewGuestStatisticalChannel(HttpServletRequest request, HttpServletResponse response, StatisticalChannelVo vo){
        try {
            if (StringUtils.isNotBlank(vo.getCreateEndTime())) {
                SimpleDateFormat df  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                vo.setCreateEndTime(df.format(DateUtils.getFirstTimeOfDay(df.parse(vo.getCreateEndTime()))));
            }
            vo.setPageIndex(1);
            vo.setPageSize(200); //默认导出200行
            //获取统计list
            List<NewGuestStatisticalChannelBo> list = newGuestStatisticalChannelService.getNewGuestStatisticalChannelList(vo);
            //excel 标题
            LinkedHashMap<String, String> propertyHeaderMap = new LinkedHashMap<>();
            propertyHeaderMap.put("createTime", "统计时间");
            propertyHeaderMap.put("phoneType", "渠道名称");
            propertyHeaderMap.put("appRegisterNum", "激活人数");
            propertyHeaderMap.put("registerNum", "注册人数");
            propertyHeaderMap.put("applicationNum", "申请人数");
            propertyHeaderMap.put("faceNum", "人脸识别人数");
            propertyHeaderMap.put("cardNum", "验证身份证人数");
            propertyHeaderMap.put("authorizationNum", "芝麻信用授权人数");
            propertyHeaderMap.put("bankCardNum", "绑定银行卡人数");
            propertyHeaderMap.put("basicNum", "基本信息填写人数");
            propertyHeaderMap.put("emergencyNum", "紧急联系人填写人数");
            propertyHeaderMap.put("auditNumber", "拍拍审核人数");
            propertyHeaderMap.put("rejectedNumber", "拍拍信被拒人数");
            propertyHeaderMap.put("successNumber", "拍拍信审核通过人数");
            propertyHeaderMap.put("openAccountNum", "提交审核人数");
            propertyHeaderMap.put("accountPassNumber", "开户通过人数");
            propertyHeaderMap.put("withdrawalNum", "提现中人数");
            propertyHeaderMap.put("withdrawalSuccessNum", "提现到账人数");
            propertyHeaderMap.put("sinceRejectedNum", "自拒人数");
            propertyHeaderMap.put("loanAmountNum", "放贷金额人数");
            propertyHeaderMap.put("chargeNum", "收取手续费人数");

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = dateFormat.format(new Date()); // 当前时间
            String title = nowDate + "渠道新客统计.xls";  // excel 名称

            response.setContentType("application/vnd.ms-excel;charset=utf-8"); // 指定文件的保存类型。
            response.setHeader("Content-Disposition", "filename=" + new String((title).getBytes("gb2312"), "iso8859-1"));
            OutputStream ouputStream = response.getOutputStream();
            //导出
            XSSFWorkbook ex = ExcelUtil.generateXlsxWorkbook(title, propertyHeaderMap, list);
            ex.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            //JSONUtils.toJSON(response, CodeUtils.QUERRYFAIL);
            return ;
        }
    }

    /**
     * excel导出渠道新客汇总
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportNewGuestSummaryChannel.do",produces = {"text/json;charset=utf-8"},method = {RequestMethod.POST, RequestMethod.GET})
    public void exportNewGuestSummaryChannel(HttpServletRequest request, HttpServletResponse response, StatisticalChannelVo vo){
        try {
            if (StringUtils.isNotBlank(vo.getSourceString())) {
                if ("iPhone".equals(vo.getSourceString())) {
                    vo.setSource(1);
                } else if ("iPad".equals(vo.getSourceString())) {
                    vo.setSource(2);
                } else if ("Android".equals(vo.getSourceString())) {
                    vo.setSource(3);
                } else if ("微信".equals(vo.getSourceString())) {
                    vo.setSource(4);
                } else if ("H5".equals(vo.getSourceString())) {
                    vo.setSource(5);
                }
            }
            if (StringUtils.isNotBlank(vo.getCreateEndTime())) {
                SimpleDateFormat df  = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                vo.setCreateEndTime(df.format(DateUtils.getFirstTimeOfDay(df.parse(vo.getCreateEndTime()))));
            }
            vo.setPageIndex(1);
            vo.setPageSize(200);
            //获取统计list
            List<NewGuestStatisticalChannelBo> list = newGuestStatisticalChannelService.getNewGuestSummaryChannelList(vo);
            //excel 标题
            LinkedHashMap<String, String> propertyHeaderMap = new LinkedHashMap<>();
            propertyHeaderMap.put("createTime", "统计时间");
            propertyHeaderMap.put("source", "渠道名称");
            propertyHeaderMap.put("appRegisterNum", "激活人数");
            propertyHeaderMap.put("registerNum", "注册人数");
            propertyHeaderMap.put("applicationNum", "申请人数");
            propertyHeaderMap.put("faceNum", "人脸识别人数");
            propertyHeaderMap.put("cardNum", "验证身份证人数");
            propertyHeaderMap.put("authorizationNum", "芝麻信用授权人数");
            propertyHeaderMap.put("bankCardNum", "绑定银行卡人数");
            propertyHeaderMap.put("basicNum", "基本信息填写人数");
            propertyHeaderMap.put("emergencyNum", "紧急联系人填写人数");
            propertyHeaderMap.put("auditNumber", "拍拍审核人数");
            propertyHeaderMap.put("rejectedNumber", "拍拍信被拒人数");
            propertyHeaderMap.put("successNumber", "拍拍信审核通过人数");
            propertyHeaderMap.put("openAccountNum", "提交审核人数");
            propertyHeaderMap.put("accountPassNumber", "开户通过人数");
            propertyHeaderMap.put("withdrawalNum", "提现中人数");
            propertyHeaderMap.put("withdrawalSuccessNum", "提现到账人数");
            propertyHeaderMap.put("sinceRejectedNum", "自拒人数");
            propertyHeaderMap.put("loanAmountNum", "放贷金额人数");
            propertyHeaderMap.put("chargeNum", "收取手续费人数");

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String nowDate = dateFormat.format(new Date()); // 当前时间
            String title = nowDate + "渠道新客汇总.xls";  // excel 名称

            response.setContentType("application/vnd.ms-excel;charset=utf-8"); // 指定文件的保存类型。
            response.setHeader("Content-Disposition", "filename=" + new String((title).getBytes("gb2312"), "iso8859-1"));
            OutputStream ouputStream = response.getOutputStream();
            //导出
            XSSFWorkbook ex = ExcelUtil.generateXlsxWorkbook(title, propertyHeaderMap, list);
            ex.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
            return ;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            //JSONUtils.toJSON(response, CodeUtils.QUERRYFAIL);
            return ;
        }
    }
}
